package in.iqing.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.iqing.app.R;
import in.iqing.base.internal.ExitIntent;
import in.iqing.base.internal.ExitIntentFilter;
import in.iqing.base.internal.LayoutHolder;
import in.iqing.control.api.ApiHelper;
import in.iqing.model.db.DBHelper;

public abstract class BaseActivity extends Activity {

    @Bind(android.R.id.content)
    public FrameLayout defaultContainer;

    @Nullable
    @Bind(R.id.content)
    public FrameLayout customContainer;

    private LayoutHolder holder;
    private IntentFilter filter;
    private BroadcastReceiver receiver;
    private SystemBarTintManager tintManager;
    private LocalBroadcastManager broadcastManager;

    protected final String TAG = getClass().getName();
    protected final String VOLLEY_TAG = TAG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.filter = new ExitIntentFilter();
        this.receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                finish();
            }
        };
        this.broadcastManager = LocalBroadcastManager.getInstance(getContext());
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);

        ButterKnife.bind(getActivity());
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);

        ButterKnife.bind(getActivity());
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        super.setContentView(view, params);

        ButterKnife.bind(getActivity());
    }

    protected void onInitialize(Bundle savedInstanceState) {
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        this.holder = LayoutHolder.newInstance(customContainer != null ? customContainer : defaultContainer);
        this.tintManager = new SystemBarTintManager(getActivity());
        tintManager.setStatusBarTintEnabled(true);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tintManager.setStatusBarTintColor(getResources().getColor(R.color.color_primary_dark));
        } else {
            tintManager.setStatusBarTintColor(getResources().getColor(R.color.theme));
        }
        onInitialize(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();

        broadcastManager.registerReceiver(receiver, filter);
    }

    @Override
    protected void onResume() {
        super.onResume();

        MobclickAgent.onResume(getContext());
    }

    @Override
    protected void onPause() {
        super.onPause();

        MobclickAgent.onPause(getContext());

    }

    @Override
    protected void onStop() {
        super.onStop();

        broadcastManager.unregisterReceiver(receiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ApiHelper.getSingleton().cancel(VOLLEY_TAG);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    protected Context getContext() {
        return getApplicationContext();
    }

    protected Activity getActivity() {
        return this;
    }

    protected void exit() {
        broadcastManager.sendBroadcast(new ExitIntent());
    }

    protected void showEmpty() {
        holder.showEmpty();
    }

    protected void showLoading() {
        holder.showLoading();
    }

    protected void showContent() {
        holder.showContent();
    }
}
